package com.bysj.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bysj.entity.Adv;
import com.bysj.entity.Guide;
import com.bysj.entity.News;
import com.bysj.entity.RenCai;
import com.bysj.entity.User;
import com.bysj.fenye.pagevo.BbsPageVO;
import com.bysj.service.AdvService;
import com.bysj.service.GuideService;
import com.bysj.service.NewsService;
import com.bysj.service.RenCaiService;
import com.bysj.service.UserService;
import com.bysj.service.impl.AdvServiceImpl;
import com.bysj.service.impl.GuideServiceImpl;
import com.bysj.service.impl.NewsServiceImpl;
import com.bysj.service.impl.RenCaiServiceImpl;
import com.bysj.service.impl.UserServiceImpl;
import com.ywb.messageDigest.MD5Digest;

public class InitServlet extends HttpServlet {
	private NewsService ns=new NewsServiceImpl();
	private BbsPageVO pagevo=new BbsPageVO(10);
	private UserService userService=new UserServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m=request.getParameter("method");
		ArrayList<News> zxnlist=ns.queryZuiXinNews();
		request.getSession().setAttribute("zxnl", zxnlist);
		if("index".equals(m)){
			AdvService as=new AdvServiceImpl();
			RenCaiService rs=new RenCaiServiceImpl();
			GuideService gs=new GuideServiceImpl();
			ArrayList<Adv> plist=as.queryadvByType(new Adv("1"));
		/*	ArrayList<Adv> dlist=as.queryadvByType(new Adv("2"));
			ArrayList<Adv> glist=as.queryadvByType(new Adv("3"));
			ArrayList<Adv> zlist=as.queryadvByType(new Adv("4"));*/
			request.getSession().setAttribute("pl", plist);
		/*	request.getSession().setAttribute("dx", dlist);
			request.getSession().setAttribute("gg", glist);
			request.getSession().setAttribute("zt", zlist);*/
			ArrayList<RenCai> klist=rs.queryRenCaiByType(new RenCai("1"));
			request.getSession().setAttribute("kj", klist);
		
			ArrayList<Guide> hglist=gs.queryGuideByType(new Guide("1"));
			request.getSession().setAttribute("hg", hglist);
			
			request.getRequestDispatcher("../index.jsp").forward(request, response);
		}else if("rencai".equals(m)){
			RenCaiService rs=new RenCaiServiceImpl();
			ArrayList<RenCai> klist=rs.queryRenCaiByType(new RenCai("1"));
			ArrayList<RenCai> clist=rs.queryRenCaiByType(new RenCai("2"));
			ArrayList<RenCai> ylist=rs.queryRenCaiByType(new RenCai("3"));
			ArrayList<RenCai> jlist=rs.queryRenCaiByType(new RenCai("4"));
			ArrayList<RenCai> slist=rs.queryRenCaiByType(new RenCai("5"));

			request.getSession().setAttribute("kj", klist);
			request.getSession().setAttribute("cx", clist);
			request.getSession().setAttribute("ys", ylist);
			request.getSession().setAttribute("jn", jlist);
			request.getSession().setAttribute("sq", slist);
			request.getRequestDispatcher("../rencai.jsp").forward(request, response);

		}else if("guide".equals(m)){
			GuideService gs=new GuideServiceImpl();
			ArrayList<Guide> hglist=gs.queryGuideByType(new Guide("1"));
			ArrayList<Guide> yjlist=gs.queryGuideByType(new Guide("2"));
			ArrayList<Guide> jzlist=gs.queryGuideByType(new Guide("3"));
			ArrayList<Guide> zclist=gs.queryGuideByType(new Guide("4"));
			ArrayList<Guide> ldlist=gs.queryGuideByType(new Guide("5"));
			request.getSession().setAttribute("hg", hglist);
			request.getSession().setAttribute("yj", yjlist);
			request.getSession().setAttribute("jz", jzlist);
			request.getSession().setAttribute("zc", zclist);
			request.getSession().setAttribute("ld", ldlist);
			request.getRequestDispatcher("../guide.jsp").forward(request, response);
		}else if("adv".equals(m)){
			AdvService as=new AdvServiceImpl();
			ArrayList<Adv> plist=as.queryadvByType(new Adv("1"));
			ArrayList<Adv> dlist=as.queryadvByType(new Adv("2"));
			ArrayList<Adv> glist=as.queryadvByType(new Adv("3"));
			ArrayList<Adv> zlist=as.queryadvByType(new Adv("4"));
			request.getSession().setAttribute("pl", plist);
			request.getSession().setAttribute("dx", dlist);
			request.getSession().setAttribute("gg", glist);
			request.getSession().setAttribute("zt", zlist);
			request.getRequestDispatcher("../adv.jsp").forward(request, response);
		}else if("bbs".equals(m)){
			String m1=request.getParameter("m1");
			String method=request.getParameter("method1");
			if("pre".equals(method)){
				this.pagevo.setPrevious();
			}else if("next".equals(method)){
				this.pagevo.setNext();
			}else if("last".equals(method)){
				this.pagevo.setLast();
			}else if("index".equals(method)){
				String ind=request.getParameter("ind");
				this.pagevo.setIndex(Integer.parseInt(ind.trim()));
			}else{
				this.pagevo.setFirst();
			}
			request.getSession().setAttribute("Blist", pagevo);
			request.getRequestDispatcher("../bbs.jsp").forward(request, response);
		}else if("userLogin".equals(m)){
			String logemail=request.getParameter("userName");
			String logpasswd=request.getParameter("password");
			User u=new User();
			u.setEmail(logemail);
			u.setPasswd(MD5Digest.getDigest(logpasswd));
			boolean b=userService.checkUserLogin(u);
			User user=userService.queryUserByemailAndpasswd(u);
			if(b){
				AdvService as=new AdvServiceImpl();
				ArrayList<Adv> plist=as.queryadvByType(new Adv("1"));
				ArrayList<Adv> dlist=as.queryadvByType(new Adv("2"));
				ArrayList<Adv> glist=as.queryadvByType(new Adv("3"));
				ArrayList<Adv> zlist=as.queryadvByType(new Adv("4"));
				request.getSession().setAttribute("pl", plist);
				request.getSession().setAttribute("dx", dlist);
				request.getSession().setAttribute("gg", glist);
				request.getSession().setAttribute("zt", zlist);
				request.getSession().setAttribute("user", user);
				request.getRequestDispatcher("../index.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("../login.jsp").forward(request, response);
			}

		}else if("personCenter".equals(m)){
			Integer id=Integer.parseInt(request.getParameter("id"));
			UserService us=new UserServiceImpl();
			User use=new User();
			use.setId(id);
			User uc=us.queryUser(use);
			request.getSession().setAttribute("uc", uc);
			request.getRequestDispatcher("./../person-center.jsp").forward(request, response);
		}else if("updateUserInfo".equals(m)){
			Integer id=Integer.parseInt(request.getParameter("id"));
			String name=request.getParameter("name");
			String address=request.getParameter("address");
			String birthday=request.getParameter("birthday");
			String email=request.getParameter("email");
			String tel=request.getParameter("tel");
			String nation=request.getParameter("nation");
			String education=request.getParameter("education");
			String school=request.getParameter("school");
			String describeself=request.getParameter("userFCKeditor");
			String sex=request.getParameter("sex");
			System.out.println(sex+"---line number 151");
			UserService us=new UserServiceImpl();
			User use=new User();
			use.setId(id);
			use.setName(name);
			use.setAddress(address);
			use.setEmail(email);
			use.setSex(sex);
			use.setBirthday(birthday);
			use.setNation(nation);
			use.setSchool(school);
			use.setDescribeself(describeself);
			use.setTel(tel);
			use.setEducation(education);
			us.updateUser(use);
			User uc=us.queryUser(use);
			request.getSession().setAttribute("uc", uc);
			request.getRequestDispatcher("../person-center.jsp").forward(request, response);
		}
	}

}
